package com.book.items.composite;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: TODO
 * @date 2023/11/12 9:36
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProductComposite extends AbstractProductItem{

    private int id;
    private int pid;
    private String name;

    private List<AbstractProductItem> child = new ArrayList<>();


    @Override
    public void addProductItem(AbstractProductItem item) {
        child.add(item);
    }

    @Override
    public void delProductItem(AbstractProductItem item) {
        ProductComposite removeItem = (ProductComposite) item;
        Iterator<AbstractProductItem> iterator = child.iterator();
        while(iterator.hasNext()){
            ProductComposite productItem = (ProductComposite)iterator.next();
            if(removeItem.getId() == productItem.getId()){
                iterator.remove();
                break;
            }
        }
    }
}
